package com.ruoyi.order.cron;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.OrderNumberGeneratorUtil;
import com.ruoyi.dynamic.domain.UserDynamic;
import com.ruoyi.dynamic.domain.UserDynamicTime;
import com.ruoyi.dynamic.mapper.UserDynamicTimeMapper;
import com.ruoyi.dynamic.service.IUserDynamicService;
import com.ruoyi.order.domain.OrderInfo;
import com.ruoyi.order.domain.OrderTask;
import com.ruoyi.order.mapper.OrderInfoMapper;
import com.ruoyi.order.mapper.OrderTaskMapper;
import com.ruoyi.problem.domain.ProblemAnswer;
import com.ruoyi.problem.mapper.ProblemAnswerMapper;
import com.ruoyi.thread.TaskAsync;
import com.ruoyi.user.domain.UserCapitalFlow;
import com.ruoyi.user.service.IUserCapitalFlowService;
import com.ruoyi.user.service.IUserTotalCapitalService;
import cn.hutool.core.collection.CollectionUtil;

/**
 * 	订单定时任务Cron
 * @author DEREK
 *
 */
@Component
@EnableScheduling
@Async
public class OrderTaskCron {

	@Autowired
	private OrderTaskMapper orderTaskMapper;
	@Autowired
	private OrderInfoMapper orderInfoMapper;
	@Autowired
	private TaskAsync taskAsync;
	@Autowired
	private IUserTotalCapitalService userTotalCapitalService;
	@Autowired
	private IUserCapitalFlowService userCapitalFlowService;
	@Autowired
	private ProblemAnswerMapper problemAnswerMapper;
	@Autowired
	private UserDynamicTimeMapper userDynamicTimeMapper;
	@Autowired
	private IUserDynamicService userDynamicService;
	
	/**
	 * 	定时任务处理各种业务数据
	 */
	@Scheduled(cron = "0 0/1 * * * ?") //1分钟执行一次
	public void updOrderStatus() {
		List<OrderTask> list = orderTaskMapper.selectList(new LambdaQueryWrapper<OrderTask>().eq(OrderTask::getStatus, 0).lt(OrderTask::getEndTime, new Date()));
		if(CollectionUtil.isNotEmpty(list)) {
			list.stream().forEach(task ->{
				if(1 == task.getType()) {
					task.setStatus(1);
					task.setUpdateTime(new Date());
					// 修改订单状态
					OrderInfo orderInfo = orderInfoMapper.selectOne(new LambdaQueryWrapper<OrderInfo>().eq(OrderInfo::getOrderNo, task.getOrderNo()).select(OrderInfo::getId));
					if(null == orderInfo) {
						task.setRemark(task.getRemark()+"-订单号未找到order:"+task.getOrderNo());
						orderTaskMapper.updateById(task);
						return;
					}
					task.setRemark(task.getRemark()+"-订单15分钟未支付正常结束");
					orderTaskMapper.updateById(task);
					orderInfo.setOrderStatus("6");
					orderInfo.setOrderUpdateTime(new Date());
					orderInfoMapper.updateById(orderInfo);
				}else if(2 == task.getType()) { // 语音通话5分钟提醒
					task.setStatus(1);
					task.setUpdateTime(new Date());
					task.setRemark(task.getRemark()+"-语音通话5分钟提醒正常结束");
					orderTaskMapper.updateById(task);
					// 推送消息给前端,播放语音
					taskAsync.sendSystemMsg(task.getUserId(), 0L, 2,"","");
					
				}else if(3 == task.getType()) { // 语音通话结束
					// 主动销毁
					task.setStatus(1);
					task.setUpdateTime(new Date());
					task.setRemark(task.getRemark()+"-余额不足,语音通话正常结束");
					orderTaskMapper.updateById(task);
					taskAsync.destructionRoom(task.getOrderNo());
					// 推送消息
					//userSystemMsgService.pushSystemMsg(task.getUserId(), "语音通话挂断", "您的可用余额已用尽,系统已自动挂断！");
					taskAsync.sendSystemMsg(task.getUserId(), 0l, 16, "", "");
					
				}else if(4 == task.getType()) {	// 24h自动采纳第一条回复
					task.setStatus(1);
					task.setUpdateTime(new Date());
					task.setRemark(task.getRemark()+"-采纳第一条回复正常结束");
					orderTaskMapper.updateById(task);
					userTotalCapitalService.initUserTotalCapital("1", task.getToUserId(), new BigDecimal(task.getOrderNo()), 1);
					UserCapitalFlow userCapitalFlow = new UserCapitalFlow();
					userCapitalFlow.setType(4);
					userCapitalFlow.setBusinessType(3);
					userCapitalFlow.setAmount(new BigDecimal(task.getOrderNo()));
					userCapitalFlow.setUserId(task.getToUserId());
					userCapitalFlow.setToUserId(task.getUserId());
					userCapitalFlow.setOrderNo(OrderNumberGeneratorUtil.get());
					userCapitalFlow.setRemark("回复用户问答被采纳-24h自动采纳");
					userCapitalFlow.setRelatedId(Long.valueOf(task.getRelatedId()));
					userCapitalFlowService.save(userCapitalFlow);
					ProblemAnswer answer = new ProblemAnswer();
					answer.setId(Long.valueOf(task.getRelatedId()));
					answer.setIsAdopt(1);
					answer.setUpdateTime(new Date());
					problemAnswerMapper.updateById(answer);
					//taskAsync.addProblemAnswerMsg(task.getUserId(), task.getToUserId(), 2);
					 answer = problemAnswerMapper.selectOne(new LambdaQueryWrapper<ProblemAnswer>().eq(ProblemAnswer::getId, answer.getId()).select(ProblemAnswer::getId,
								ProblemAnswer::getIsAdopt,ProblemAnswer::getCid,ProblemAnswer::getUserId));
						if(null != answer) {
							taskAsync.sendSystemMsg(answer.getUserId(), 0l, 6, String.valueOf(answer.getCid()), "");
						}
				}else if(6 == task.getType()) {
					Integer dynamicCount = userDynamicService.count(new LambdaQueryWrapper<UserDynamic>().eq(UserDynamic::getId, task.getRelatedId()).eq(UserDynamic::getStatus, 0));
					if(dynamicCount < 1) {
						task.setStatus(1);
						task.setUpdateTime(new Date());
						task.setRemark(task.getRemark()+"-此活动审核拒绝或被删除,未推送消息");
						orderTaskMapper.updateById(task);
						return;
					}
					task.setStatus(1);
					task.setUpdateTime(new Date());
					task.setRemark(task.getRemark()+"-正常结束");
					orderTaskMapper.updateById(task);
					List<UserDynamicTime> timeList = userDynamicTimeMapper.selectList(new LambdaQueryWrapper<UserDynamicTime>().eq(UserDynamicTime::getDynamicId, task.getRelatedId())
							.isNotNull(UserDynamicTime::getReservationUserId));
					if(CollectionUtil.isNotEmpty(timeList)) {
						timeList.stream().forEach(time ->{
							taskAsync.sendSystemMsg(time.getReservationUserId(), 0l, 11, String.valueOf(task.getRelatedId()),task.getExtend());
						});
					}
					taskAsync.sendSystemMsg(task.getUserId(), 0l, 12, String.valueOf(task.getRelatedId()),task.getOrderNo());
				}else if(7 == task.getType()) { // 定时发送消息 
					task.setStatus(1);
					task.setUpdateTime(new Date());
					task.setRemark(task.getRemark()+"-正常结束");
					orderTaskMapper.updateById(task);
					taskAsync.newUserRegisterSendMsg(task.getUserId(), task.getToUserId(), task.getExtend());
				}
			});
		}
	}
}
